<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="创建人" prop="logOperateBy">
        <el-select v-model="queryParams.logOperateBy" placeholder="请选择创建人" clearable @change="handleQuery">
          <el-option v-for="user in userList" :key="user.userId" :label="user.userName" :value="user.userId" />
        </el-select>
      </el-form-item>
      <el-form-item label="设备名称" prop="logReId">
        <el-select v-model="queryParams.logReId" placeholder="请选择创建人" clearable @change="handleQuery">
          <el-option v-for="re in reList" :key="re.reId" :label="re.reName" :value="re.reId" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"
          v-hasPermi="['eamp:re_logs:remove']">删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
          v-hasPermi="['eamp:re_logs:export']">导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="re_logsList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="编号" align="center" prop="logId" />
      <el-table-column label="创建人" align="center" prop="logOperateBy">
        <template slot-scope="scope">
          {{ getUserName(scope.row.logOperateBy).userName }}
        </template>
      </el-table-column>
      <el-table-column label="创建时间" align="center" prop="logOperateTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.logOperateTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作名称" align="center" prop="logMethodName">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.eamp_sr_use_type" :value="scope.row.logMethodName" />
        </template>
      </el-table-column>
      <el-table-column label="设备名称" align="center" prop="logReId">
        <template slot-scope="scope">
          {{ getReList(scope.row.logReId).reName }}
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-view" @click="handleView(scope.row)"
            v-hasPermi="['eamp:re_logs:remove']">查看</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
            v-hasPermi="['eamp:re_logs:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <!-- 查看设备记录对话框 -->
    <el-dialog :title="title" :visible.sync="ViewDialog" width="500px" append-to-body>
      <el-form ref="viewForm" :model="viewForm" :rules="rules" label-width="80px">
        <el-form-item label="设备名称" prop="reName">
          <el-input :value="viewForm.eampReMan && viewForm.eampReMan.reName" readonly disabled />
        </el-form-item>
        <el-form-item label="所属部门" prop="reDeptId">
          <el-input :value="viewForm.sysDept && viewForm.sysDept.deptName" readonly disabled />
        </el-form-item>
        <el-form-item label="设备型号" prop="reModel">
          <el-input :value="viewForm.eampReMan && viewForm.eampReMan.reModel" readonly disabled />
        </el-form-item>
        <el-form-item label="设备状态" prop="reState">
          <el-input :value="viewForm.eampReMan && viewForm.eampReMan.reStateName" readonly disabled />
        </el-form-item>
        <el-form-item label="存放位置" prop="reSrId">
          <el-input :value="viewForm.eampSrMan && viewForm.eampSrMan.srName" readonly disabled />
        </el-form-item>
        <!-- <el-form-item label="附件" prop="reName">
          <el-input v-model="viewForm.reFile" readonly disabled/>
        </el-form-item> -->
        <el-form-item label="备注" prop="reRemark">
          <el-input :value="viewForm.eampReMan && viewForm.eampReMan.reRemark" type="textarea" readonly disabled />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listRe_logs, delRe_logs, listVO, listUser, listRe, } from "@/api/eamp/re_logs";
import {  getDicts,  } from "@/api/system/dict/data";
export default {
  name: "Re_logs",
  dicts: ['eamp_sr_type', 'eamp_sr_use_type','eamp_re_state'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 设备全部记录表格数据
      re_logsList: [],
      // 全部的vo
      vo_list: [],
      // 用户列表
      userList: [],
      //设备列表
      reList: [],

      typeDictList:[],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      ViewDialog: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        logOperateBy: null,
        logReId: null,
        delFlg: 1
      },
      // 表单参数
      form: {},
      viewForm: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
    this.getVOList();
    this.getUserAndReList();
    this.getDictList();
  },
  methods: {
    getDictList(){
      getDicts("eamp_re_state").then(res => {
        this.typeDictList = res.data;
        // console.log(this.typeDictList)
      });
    },
    getDictName(reState){   
      const dict = this.typeDictList.find(dict => dict.dictValue == reState);
      // console.log(dict);
      return dict ? dict.dictLabel : '未知';
    },
    /* 查询用户和设备列表 */
    getUserAndReList() {
      listUser().then(res => {
        // console.log(res);  
        this.userList = res.data;
      })
      listRe().then(res => {
        this.reList = res.data;
        // console.log(res);

      })
    },
    getUserName(logOperateBy) {
      const user = this.userList.find(item => item.userId == logOperateBy);
      return user ? user : '';
    },
    /* 查询VO */
    getVOList() {
      listVO().then(res => {
        this.vo_list = res.rows;
      })
    },
    getReList(logReId) {
      const re = this.vo_list.find(item => item.eampReMan.reId == logReId);
      // console.log(re);
      return re ? re.eampReMan : ''
    },
    /** 查询设备全部记录列表 */
    getList() {
      this.loading = true;
      listRe_logs(this.queryParams).then(response => {
        this.re_logsList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.ViewDialog = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        logId: null,
        logOperateBy: null,
        logOperateTime: null,
        logMethodName: null,
        logReId: null,
        delFlg: 1
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.logId)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /* 查看按钮 */
    handleView(row) {
      const reInfo = this.getVO(row.logReId);
      if (reInfo) {
        // console.log(reInfo);
        console.log(reInfo.eampReMan.reState);
        reInfo.eampReMan.reStateName = this.getDictName(reInfo.eampReMan.reState)
        this.viewForm = reInfo;
      }
      this.ViewDialog = true;
    },
    getVO(id) {
      return this.vo_list.find(item => item.eampReLogs.logReId === id) || null;
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const logIds = row.logId || this.ids;
      this.$modal.confirm('是否确认删除设备全部记录编号为"' + logIds + '"的数据项？').then(function () {
        return delRe_logs(logIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => { });
      this.getList();
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('eamp/re_logs/export', {
        ...this.queryParams
      }, `re_logs_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
